﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using RazakiControls.Classes;

namespace RazakiControls
{
    [DefaultProperty("Caption")]
    [ToolboxData("<{0}:RazakiDropDownList runat=server></{0}:RazakiDropDownList>")]
    [ToolboxBitmap(typeof(RazakiDropDownList), "Images.DefaultControl.ico")]
    public class RazakiDropDownList : DropDownList
    {
        private Label _label = new Label();

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Caption
        {
            get
            {
                var caption = (string)ViewState["Caption"];
                return (caption ?? String.Empty);
            }
            set
            {
                ViewState["Caption"] = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!string.IsNullOrWhiteSpace(Caption))
            {
                _label.Width = Utilities._CAPTION_WIDTH;
                _label.Text = string.Format("{0}: ", Caption);

                Controls.Add(_label);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //#1
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            //#2
            if (!string.IsNullOrWhiteSpace(Caption))
            {
                _label.RenderControl(writer);
            }

            //#3
            base.Render(writer);

            //end #1
            writer.RenderEndTag();
        }

        protected override ControlCollection CreateControlCollection()
        {
            return new ControlCollection(this);
        }
    }
}